﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using MyDMS.DAL;

public partial class MyDrafts : PageBaseClass
{
    private MyDMSEntity _myDmsEntity = new MyDMSEntity();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Department> deptList =
                _myDmsEntity.Department.Where(d => d.Office_Code == Author.office_Code).ToList();

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = "Department_Name";
            ddlDept.DataValueField = "Department_Code";
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
            {
                BindTheUsers(0);
            }
        }

        List<MailMessage> msgs = _myDmsEntity.MailMessage.Where(mm => mm.Owner == Author.user_Code).ToList();

        gvMessages.DataSource = msgs;
        gvMessages.DataBind();

        BindTheUD(Author.user_Code);
        //DocumentTypeFactory docTypeFactory = new DocumentTypeFactory();
        //List<DocumentType> docTypes = docTypeFactory.GetAllBy(DocumentType.DocumentTypeFields.Office_Code, _author.office_Code);

        //uDocFactory = new UploadedDocumentFactory();
        //List<Document> uDocs = uDocFactory.GetAllBy(Document.UploadedDocumentFields.Owner, _author.user_Code);

        //gvUploadedDocs.DataSource = uDocs;
        //gvUploadedDocs.DataBind();
    }

    private void BindTheUsers(int deptCode)
    {
        if (deptCode >= 0)
        {
            this.ddlUsers.DataSource = _myDmsEntity.User.Where(ui => ui.Department_Code == deptCode).ToList();
        }
        else
        {
            this.ddlUsers.DataSource = _myDmsEntity.User.ToList();
        }

        this.ddlUsers.DataTextField = "LastName";// +", " + UserInfo.UserInfoFields.LastName.ToString();
        this.ddlUsers.DataTextFormatString = "Ms {0}";
        this.ddlUsers.DataValueField = "User_Code";
        this.ddlUsers.DataBind();
    }

    private void BindTheUD(int userCode)
    {
        //object dataObject = null;
        //new SqlFactory().ExecuteQuery("MAN_Select_UploadedDocumentByOwner " + userCode, out dataObject, SqlFactory.DataObjectType.DataSet);

        //if (dataObject != null)
        //{

        gvUploadedDocs.DataSource = _myDmsEntity.Document.Where(ud => ud.Owner == userCode).ToList();
            gvUploadedDocs.DataBind();
        //}
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            BindTheUsers(int.Parse(ddlDept.SelectedValue));
        }
        else
        {
            BindTheUsers(0);
        }
    }

    protected void btnShare_Click(object sender, EventArgs e)
    {
        if (hdfItemType.Value == "Message")
        {
            UserMessage uMsg = new UserMessage();
            uMsg.Message_Code = int.Parse(hdfItemCode.Value);
            uMsg.User_Code = int.Parse(ddlUsers.SelectedValue);
            uMsg.Is_Starred = false;
            uMsg.Is_Read = false;
            uMsg.Is_Deleted = false;
            uMsg.Creation_Date = DateTime.Now;
            uMsg.Updation_Date = DateTime.Now;

            _myDmsEntity.UserMessage.AddObject(uMsg);

            if (_myDmsEntity.SaveChanges()>0)
            {
                lblStatus.Text = "Shared with " + ddlUsers.SelectedItem.Text;
            }
            else
            {
                lblStatus.Text = "Not Shared with " + ddlUsers.SelectedItem.Text;
            }
        }
        else if (hdfItemType.Value == "Document")
        {
            UserDocument uDoc = new UserDocument();
            uDoc.User_Code = int.Parse(ddlUsers.SelectedValue);
            uDoc.Document_Code = int.Parse(hdfItemCode.Value);
            uDoc.Shared_By = Author.user_Code;
            uDoc.Shared_Date = DateTime.Now;
            uDoc.Share_Comment = "Check this out!";

            _myDmsEntity.UserDocument.AddObject(uDoc);

            if (_myDmsEntity.SaveChanges()>0)
            {
                lblStatus.Text = "Shared with " + ddlUsers.SelectedItem.Text;
            }
            else
            {
                lblStatus.Text = "Not Shared with " + ddlUsers.SelectedItem.Text;
            }
        }
        btnShare.Style["display"] = "inline";
        lblStatus.Visible = true;
    }
}
